/*
 * 整体妆容的分类管理创建
 * @Author: zfx
 * @Date: 2018-06-26 17:52:07
 */
<style lang="less" rel="stylesheet/less" type="text/less" scoped>
    .v-customer-classify-create {
		min-height: 74vh;
		overflow-y: scroll;
		&::-webkit-scrollbar {
			display: none;
		}
        .classify-form {
            width: 350px;
        }
    }
</style>
<template>
	<div class="v-customer-classify-create">
		<Row
			type="flex"
			justify="start">
			<i-col push="1">
				<Form
					ref="formValidate"
					:model="formValidate"
					:rules="ruleValidate"
					:label-width="labelWidth(80, 80)"
					class="create__form">

					<Form-item
						:label="$t('classification.name')"
						prop="name">
						<i-input
							v-model="formValidate.name"
							:placeholder="$t('classification.please_enter_name')"
							name="create__name"
							type="text"/>
					</Form-item>

					<!-- <Form-item
						:label="$t('classification.thumb')"
						prop="thumb">
						<Uploader
							ref="thumb"
							@on-remove="(file) => {handleRemove('thumb', file)}"
							@on-change="(file) => {handleChange('thumb', file)}">
							{{ $t('classification.pic_tip') }}
						</Uploader>
					</Form-item> -->

					<Form-item
						:label="$t('classification.priority')"
						prop="priority">
						<InputNumber
							:max="100000"
							:min="0"
							v-model="formValidate.priority"
							name="create__priority"/>
					</Form-item>

					<Form-item>
						<Button
							type="primary"
							name="create__submit"
							@click="handleSubmit('formValidate')">
							{{ $t('common.submit') }}
						</Button>

						<Button
							type="ghost"
							name="create__reset"
							style="margin-left: 8px"
							@click="handleReset('formValidate')">
							{{ $t('common.reset') }}
						</Button>

					</Form-item>

				</Form>
			</i-col>
		</Row>
	</div>
</template>

<script>

import labelWidthMixin from 'src/views/mixins/labelWidthMixin';
import { validateImage } from 'src/validate'; // 引入校验文件
import Uploader from 'src/views/component/Uploader';

export default {
	name: 'ClassificationCreate',
	components: {
		Uploader,
	},
	mixins: [labelWidthMixin],
	props: {
		loading: {
			type: Boolean,
			default() {
				return false;
			},
		},
	},

	data() {
		return {
			formValidate: {
				name: '', // 分类名称

				priority: 0, // 优先级

				// thumb: null, // 缩略图
			}, // 新建分类时的表单

			ruleValidate: { // 表单验证规则
				name: [
					{
						required: true,
						message: this.$t('validate.required', { field: this.$t('classification.name') }),
						trigger: 'blur,change',
					},
				],
				thumb: [
					{
						required: true,
						validator: validateImage,
						trigger: 'blur,change',
					},
				],
				priority: [
					{
						required: true,
						type: 'number',
						message: this.$t('validate.required', { field: this.$t('classification.priority') }),
						trigger: 'blur,change',
					},
				],

			},

		};
	},

	watch: {

	},
	methods: {

		/**
		 * 选择图片
		 * @param {Object} file {选择的文件对象集合}
		 * @param {String} field {icon}
		 * @return {undefined}
		 */
		handleChange(field, { file }) {
			this.formValidate[field] = file;
			this.$refs.formValidate.validateField(field);
		},

		/**
		 * 删除图片
		 * @param {String} field {icon}
		 * @return {undefined}
		 */
		handleRemove(field) {
			this.formValidate[field] = null;
			this.$refs.formValidate.validateField(field);
		},

		/**
         * 提交表单
         * @param  {string} name {表单的ref}
         * @return {undefined}
         */
		handleSubmit(name) {
			if (this.loading) return;
			this.$refs[name].validate((valid) => {
				if (valid) {
					this.$emit('post', this.formDataCreate());
				} else {
					this.$Message.error(this.$t('common.exist_empty_field'));
				}
			});
		},

		/**
         * 重置表单
         * @param  {string} name {表单的ref}
         * @return {undefined}
         */
		handleReset(name) {
			// this.$refs.thumb.reset();
			this.$refs[name].resetFields();
		},

		/**
         * 手动拼接表单数据
         * @return {Object} {拼接好的表单数据}
         */
		formDataCreate() {
			const new_object = {
				name: this.formValidate.name,

				// image: this.formValidate.thumb,

				priority: this.formValidate.priority,
			};

			const form_data = new FormData();

			Object.keys(new_object).forEach((key) => {
				if (typeof new_object[key] !== 'undefined') {
					form_data.append(key, new_object[key]);
				}
			});

			return form_data;
		},

	},


};
</script>
